package cn.sumpu.app.wlcity.dao.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.sumpu.app.wlcity.dao.VoteDao;
import cn.sumpu.app.wlcity.domain.Vote;
import cn.sumpu.app.wlcity.domain.VoteItem;
import cn.sumpu.app.wlcity.domain.VoteItemPhone;
import cn.sumpu.app.wlcity.exception.AppBusinessException;

public class VoteDaoImpl extends SqlMapClientDaoSupport implements VoteDao {

	private static Logger logger = LoggerFactory.getLogger(VoteDaoImpl.class);

	@Override
	public void insertVote(Vote vote) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().insert("vote.insertVote", vote);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public void deleteVote(String identifier) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().delete("vote.deleteVote", identifier);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
	}

	@Override
	public void insertVoteItem(VoteItem voteItem) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().insert("vote.insertVoteItem", voteItem);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}

	}

	@Override
	public void insertVoteItemPhone(VoteItemPhone voteItemPhone)
			throws AppBusinessException {
		try {
			getSqlMapClientTemplate().insert("vote.insertVoteItemPhone",
					voteItemPhone);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Vote> getVoteAndVoteItemList(Map<String, Object> map) throws AppBusinessException {
		try {
			return (List<Vote>) getSqlMapClientTemplate().queryForList(
					"vote.getVoteAndVoteItemList",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Vote> getVoteList(Map<String, Object> map) throws AppBusinessException {
		try {
			return (List<Vote>) getSqlMapClientTemplate().queryForList(
					"vote.getVoteList",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}
	@Override
	public void updateVote(Vote vote) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update("vote.updateVote", vote);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
	}

	@Override
	public Vote getVoteById(String id) throws AppBusinessException {
		try {
			return (Vote) getSqlMapClientTemplate().queryForObject(
					"vote.getVoteById", id);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}

	@Override
	public VoteItem getVoteItemById(String id) throws AppBusinessException {
		try {
			return (VoteItem) getSqlMapClientTemplate().queryForObject(
					"vote.getVoteItemById", id);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}

	@Override
	public void updateVoteItem(VoteItem voteItem) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update("vote.updateVoteItem", voteItem);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
		
	}

	@Override
	public void upEachVoteSort(String app) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update("vote.upEachVoteSort",app);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
		
	}

	@Override
	public void moveVoteSort(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update("vote.moveVoteSort", map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
		
	}

	@Override
	public int getVoteListCount(String app) throws AppBusinessException {
		try {
			return  (Integer) getSqlMapClientTemplate().queryForObject("vote.getVoteListCount",app);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}

	@Override
	public void deleteVoteItemList(String identifier)
			throws AppBusinessException {
		try {
			getSqlMapClientTemplate().delete("vote.deleteVoteItemList", identifier);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
		
	}

	@Override
	public void downEachVoteSort(Map<String, Object> map) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update("vote.downEachVoteSort", map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
		
	}

	@Override
	public VoteItemPhone checkVotePhone(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (VoteItemPhone) getSqlMapClientTemplate().queryForObject(
					"vote.checkVotePhone", map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<VoteItem> getVoteItemList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<VoteItem>) getSqlMapClientTemplate().queryForList(
					"vote.getVoteItemList",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}

	@Override
	public void deleteVoteItem(String identifier) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().delete("vote.deleteVoteItem", identifier);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
		
	}

	@Override
	public void deleteVoteItemPhone(String voteItemId)
			throws AppBusinessException {
		try {
			getSqlMapClientTemplate().delete("vote.deleteVoteItemPhone", voteItemId);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
		
	}

	@Override
	public int getVoteItemListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return  (Integer) getSqlMapClientTemplate().queryForObject("vote.getVoteItemListCount",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VoteItemPhone> getVotePhone(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<VoteItemPhone>) getSqlMapClientTemplate().queryForList(
					"vote.getVotePhone",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("getVotePhone failure.", e);
			}
			throw new AppBusinessException("getVotePhone failure.", e);
		}
	}

	@Override
	public int getVotePhoneCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return  (Integer) getSqlMapClientTemplate().queryForObject("vote.getVotePhoneCount",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("getVotePhoneCount failure.", e);
			}
			throw new AppBusinessException("getVotePhoneCount failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Vote> getVote(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<Vote>) getSqlMapClientTemplate().queryForList(
					"vote.getVote",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("getVote failure.", e);
			}
			throw new AppBusinessException("getVote failure.", e);
		}
	}

	@Override
	public int getVoteCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return  (Integer) getSqlMapClientTemplate().queryForObject("vote.getVoteCount",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("getVoteCount failure.", e);
			}
			throw new AppBusinessException("getVoteCount failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VoteItem> getVoteItem(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<VoteItem>) getSqlMapClientTemplate().queryForList(
					"vote.getVoteItem",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("getVoteItem failure.", e);
			}
			throw new AppBusinessException("getVoteItem failure.", e);
		}
	}

	@Override
	public int getVoteItemCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return  (Integer) getSqlMapClientTemplate().queryForObject("vote.getVoteItemCount",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("getVoteItemCount failure.", e);
			}
			throw new AppBusinessException("getVoteItemCount failure.", e);
		}
	}

}
